module jinji.common.manage;

import jinji.common.query,
jinji.post.model,
jinji.tag.model,
jinji.user.model;

/// 删除所有
@"删除行数"@type("[number]")
@Action void deleteAll(ref Request req, @"表名" string table) {
	Perm perm;
	switch (table) {
	case ChangeRequest.stringof:
		perm = Perm.editCR;
		break;
	case Classifier.stringof:
		perm = Perm.editBot;
		break;
	case Tag.stringof:
		perm = Perm.editTag;
		break;
	case TagAlias.stringof:
		perm = Perm.editTA;
		break;
	case TagCategory.stringof:
		perm = Perm.editTC;
		break;
	case TagGroup.stringof:
		perm = Perm.editTG;
		break;
	case TagImplications.stringof:
		perm = Perm.editTI;
		break;
	default:
		throw new ArgOutOfRangeEx(_("不支持的表"));
	}
	req.require(perm);
	db.exec(`DELETE FROM ` ~ quote(snakeCase(table)));
	req.send(db.totalChanges);
}

/// 重建所有FTS索引
@Action void rebuildIndex(ref Request req) {
	import jinji.db;

	uid(req);
	rebuildFTS!"tagidx"();
}

/// 清除过期的数据
void cleanExpired() {
	/// 清除过期的可见性规则
	db.delWhere!(PostVisibility, "end_at<=now()");
	db.delWhere!(TAVisibility, "end_at<=now()");
	db.delWhere!(TGVisibility, "end_at<=now()");
	db.delWhere!(TIVisibility, "end_at<=now()");
	// 清除过期token
	db.delWhere!(RevokedToken, "expires_at<now()");
}
